<?php
$state = "";
$latitude = "";
$longitude = "";
function startElement($parser,$name,$attrib){
  global $state;
   switch ($name) {
   case $name=="latitude" : {
     $state = $name;
   }
   case $name == "longitude" : {
     $state = $name;
 }
 }
}
function endElement($parser,$name,$attrib){
  global $state;
  global $latitude;
  global $longitude;

  $state = "";
}
function characters($parser,$xml_data){
  global $state;
  global $latitude;
  global $longitude;
  
  switch ($state) {
  case $state=="latitude" : {
    $state = $name;
    $latitude = $xml_data;
  }
  case $state == "longitude" : {
    $state = $name;
    $longitude = $xml_data;
 }
  }
}

$response = array();
if (isset($_GET['team']) && isset($_GET['type'])){
  $url = "http://www.nba.com/scores/calendarJSON2.jsp?team=".$_GET['team']."&season=2008";
  $output = file_get_contents($url); 
  $output =  preg_replace("/\"nattv\".*?,/", "", $output);
  $jsonObj = json_decode($output);
  $xml_parser = xml_parser_create();
  xml_set_element_handler($xml_parser, "startElement", "endElement");
  xml_set_character_data_handler($xml_parser, "characters");
  if ($_GET['type'] == 1){//forward two weeks
    $today = date("m/d/Y");
    $todayArr = explode("/",$today);
    $currentminutes = date("H");
    $currentminutesArr = array("hour"=>
			       substr($currentminutes, 0,2), 
			       "minutes"=>
			       substr($currentminutes, 2, 2));
    $todayEpoch = mktime($currentminutesArr["hour"], 
			 $currentminutesArr["minutes"],
			 0,
			 $todayArr[0], 
			 $todayArr[1], 
			 $todayArr[2]);

    $twoweeksEpoch = $todayEpoch + 1209600 / 2;//add two weeks
    $matchArray = array();
    foreach($jsonObj as $property => $value){ //for each property
      if (is_numeric($property)){//it is a month
	foreach($value as $item){
	  if (!(isset($item->{date}))){//if date is not set
	    print "$item does not have date property!";
	  }
	  else{
	    $itemDate = $item->{date};
	    $gTime = $item->{gTime};
	    $gTimematches = array();
	    preg_match("/([0-9]{1,2}):([0-9]{1,2})((?:pm)|(?:am))/", $gTime, $gTimematches);
	    if ($gTimematches[3] == "pm"){
	      $gTimematches[1] = $gTimematches[1] + 12;
	    }
	    $itemDateArr = explode("/",$itemDate);
	    $itemEpoch = mktime($gTimematches[1],
				$gTimematches[2],
				0, 
				$itemDateArr[0], 
				$itemDateArr[1], 
				$itemDateArr[2]);
	    if ($itemEpoch > $todayEpoch && $itemEpoch < $twoweeksEpoch){//append the match
	      $isHome = $item->{isHome};
	      $oppCity = $item->{oppCity};
	      $teamCity = $item->{teamCity};
	      if ($isHome == "0"){
		$location = $oppCity;
	      }
	      else{
		$location = $teamCity;
	      }
	      $location=preg_replace("/\s/", "%22", $location);
	      $geocode = "http://local.yahooapis.com/MapsService/V1/geocode?appid=iK0KW3PV34EtgHJ7Y_KTQe_fzqU2q7HLkox3TAiXGBlB5.8hRawN5o4D9MZB&location=" . $location . "&output=php";		
	      $geocodeResponse= file_get_contents($geocode); 
	      $geoCodeXML = unserialize($geocodeResponse);
	      $item->{"Latitude"} = $geoCodeXML["ResultSet"]["Result"]["Latitude"];
	      $item->{"Longitude"} = $geoCodeXML["ResultSet"]["Result"]["Longitude"];
	      $matchArray[$itemDate] = $item;
	    }
	  }
	}
      }
    }
  }
  else if ($_GET['type'] == 2){
  $today = date("m/d/Y");
    $todayArr = explode("/",$today);
    $currentminutes = date("H");
    $currentminutesArr = array("hour"=>
			       substr($currentminutes, 0,2), 
			       "minutes"=>
			       substr($currentminutes, 2, 2));
    $todayEpoch = mktime($currentminutesArr["hour"], 
			 $currentminutesArr["minutes"],
			 0,
			 $todayArr[0], 
			 $todayArr[1], 
			 $todayArr[2]);

    $twoweeksEpoch = $todayEpoch - 1209600 / 2;//minus two weeks
    $matchArray = array();
    foreach($jsonObj as $property => $value){ //for each property
      if (is_numeric($property)){//it is a month
	foreach($value as $item){
	  if (!(isset($item->{date}))){//if date is not set
	    print "$item does not have date property!";
	  }
	  else{
	    $itemDate = $item->{date};
	    $gTime = $item->{gTime};
	    $gTimematches = array();
	    preg_match("/([0-9]*):([0-9]*)(pm|am)/", $gTime, $gTimematches);
	    if ($gTimesmatches[3] == "pm"){
	      $gTimematches[1] = $gTimematches[1] + 12;
	    }
	    $itemDateArr = explode("/",$itemDate);
	    $itemEpoch = mktime($gTimematches[1],
				$gTimematches[2],
				0, 
				$itemDateArr[0], 
				$itemDateArr[1], 
				$itemDateArr[2]);
	    if ($itemEpoch < $todayEpoch && $itemEpoch > $twoweeksEpoch){//append the match
	      $isHome = $item->{isHome};
	      $oppCity = $item->{oppCity};
	      $teamCity = $item->{teamCity};
	      if ($isHome == "0"){
		$location = $oppCity;
	      }
	      else{
		$location = $teamCity;
	      }
	      $location=preg_replace("/\s/", "%22", $location);
	      $geocode = "http://local.yahooapis.com/MapsService/V1/geocode?appid=iK0KW3PV34EtgHJ7Y_KTQe_fzqU2q7HLkox3TAiXGBlB5.8hRawN5o4D9MZB&location=" . $location . "&output=php";		
	      $geocodeResponse= file_get_contents($geocode); 
	      $geoCodeXML = unserialize($geocodeResponse);
	      $item->{"Latitude"} = $geoCodeXML["ResultSet"]["Result"]["Latitude"];
	      $item->{"Longitude"} = $geoCodeXML["ResultSet"]["Result"]["Longitude"];
	      $matchArray[$itemDate] = $item;
	    }
	  }
	}
      }
    }
  }
  else if ($_GET['type'] == 3){
    //not implemented yet
  }
}
else{
  echo "Did not find parameters in theget array";
}
$response = json_encode($matchArray);
echo $response;
?>